package gui;

import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import security.Encryption;

import entities.User;


public class CreateAccount {
	private WindowsApplication windowsApp;
	private JPanel pnlCreateAccount;
	private JTextField txtUsername;
	private JTextField txtEmail;
	private JTextField txtEmailConfirm;
	private JPasswordField pswPassword;
	private JPasswordField pswPasswordConfirm;
	private JTextField txtFirstName;
	private JTextField txtLastName;
	
	public CreateAccount(){
		initialize();
	}
	public CreateAccount(WindowsApplication windowsApp){
		this.windowsApp = windowsApp;
		initialize();
	}
	
	private void initialize(){
		pnlCreateAccount =  new JPanel();
		pnlCreateAccount.setBounds(0, 0, 800, 600);
		pnlCreateAccount.setLayout(null);
		
		JLabel lblPleaseEnterThe = new JLabel("Please enter the following information to create an account.");
		lblPleaseEnterThe.setFont(new Font("Arial Rounded MT Bold", Font.PLAIN, 20));
		lblPleaseEnterThe.setBounds(25, 50, 750, 25);
		pnlCreateAccount.add(lblPleaseEnterThe);
		
		JLabel lblUsername = new JLabel("Username");
		lblUsername.setBounds(50, 100, 85, 22);
		pnlCreateAccount.add(lblUsername);
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setBounds(50, 135, 85, 22);
		pnlCreateAccount.add(lblPassword);
		
		JLabel lblConfirmPassword = new JLabel("Confirm Password");
		lblConfirmPassword.setBounds(50, 170, 138, 15);
		pnlCreateAccount.add(lblConfirmPassword);
		
		JLabel lblEmail = new JLabel("Email");
		lblEmail.setBounds(50, 256, 85, 22);
		pnlCreateAccount.add(lblEmail);
		
		JLabel lblConfirmEmail = new JLabel("ConfirmEmail");
		lblConfirmEmail.setBounds(50, 291, 85, 22);
		pnlCreateAccount.add(lblConfirmEmail);
		
		txtUsername = new JTextField();
		txtUsername.setBounds(200, 100, 250, 22);
		pnlCreateAccount.add(txtUsername);
		txtUsername.setColumns(10);
		
		txtEmail = new JTextField();
		txtEmail.setBounds(200, 256, 250, 22);
		pnlCreateAccount.add(txtEmail);
		txtEmail.setColumns(10);
		
		JButton btnCreateAccount = new JButton("Create Account");
		btnCreateAccount.setBounds(537, 562, 142, 25);
		btnCreateAccount.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				User user = new User();
				boolean add = true;
				
				String email = txtEmail.getText();
				if(email.trim().length() == 0)
					add = false;
				
				String emailConfirm = txtEmailConfirm.getText();
				if(emailConfirm.trim().length() == 0)
					add = false;
				
				if(add){
					if(email.equals(emailConfirm)){
						user.setEmail(email);
					}else
						add = false;
				}
				
				String password = String.valueOf(pswPassword.getPassword());
				if(password.trim().length() == 0)
					add = false;
				
				String passwordConfirm = String.valueOf(pswPasswordConfirm.getPassword());
				if(passwordConfirm.trim().length() == 0)
					add = false;
				
				if(add){
					if(password.equals(passwordConfirm)){
						user.setPassword(password);
					}else
						add = false;
				}
				
				String fname = txtFirstName.getText();
				if(fname.trim().length() == 0)
					add = false;
				
				String lname = txtLastName.getText();
				if(lname.trim().length() == 0)
					add = false;
				
				String username = txtUsername.getText();
				if(username.trim().length() == 0)
					add = false;				
				
				if(add){
					user.setUsername(username);				
					user.setFname(fname);
					user.setLname(lname);
					user.setEmail(email);
					user.setPassword(Encryption.digest(password));
					user.setRole("user");
					
					windowsApp.getCSEP().getConnectionServiceImpPort().addUser(user);
					windowsApp.setUser(windowsApp.getCSEP().getConnectionServiceImpPort().getUser(user.getUsername()));

					windowsApp.getCSEP().getConnectionServiceImpPort().addUserDevice(windowsApp.getDevice().getUUID(), String.valueOf(windowsApp.getUser().getId()));
					
					windowsApp.getFrame().setContentPane(new Overview(windowsApp).getPanel());
				}else
					windowsApp.getFrame().setContentPane(new CreateAccount(windowsApp).getPanel());
			}
		});
		pnlCreateAccount.add(btnCreateAccount);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.setBounds(691, 562, 97, 25);
		btnCancel.addActionListener(new ActionListener(){
			public void actionPerformed(ActionEvent e){
				windowsApp.getFrame().setContentPane(new Login(windowsApp).getPanel());
			}
		});
		pnlCreateAccount.add(btnCancel);
		
		pswPassword = new JPasswordField();
		pswPassword.setBounds(200, 135, 250, 22);
		pnlCreateAccount.add(pswPassword);
				
		txtEmailConfirm = new JTextField();
		txtEmailConfirm.setBounds(200, 291, 250, 22);
		pnlCreateAccount.add(txtEmailConfirm);
		txtEmailConfirm.setColumns(10);
				
		pswPasswordConfirm = new JPasswordField();
		pswPasswordConfirm.setBounds(200, 166, 250, 22);
		pnlCreateAccount.add(pswPasswordConfirm);
		
		JLabel lblFirstName = new JLabel("First Name");
		lblFirstName.setBounds(50, 198, 104, 16);
		pnlCreateAccount.add(lblFirstName);
		
		JLabel lblLastName = new JLabel("Last Name");
		lblLastName.setBounds(50, 227, 104, 16);
		pnlCreateAccount.add(lblLastName);
		
		txtFirstName = new JTextField();
		txtFirstName.setBounds(200, 195, 250, 22);
		pnlCreateAccount.add(txtFirstName);
		txtFirstName.setColumns(10);
		
		txtLastName = new JTextField();
		txtLastName.setBounds(200, 224, 250, 22);
		pnlCreateAccount.add(txtLastName);
		txtLastName.setColumns(10);
	}
	
	public JPanel getPanel(){
		return pnlCreateAccount;
	}
}
